#!/bin/sh

ROOTDIR=$(pwd)
VERSION=$1
CLIENT_TYPE=CN
PRODUCT_MODEL=B1
PACK_VERSION=${PRODUCT_MODEL}_${CLIENT_TYPE}_${VERSION}
PACK_TIME=`date +%Y%m%d%H%M%S`
UPGRADE_TOOL_NAME=upgrade_generate

ECHO()
{
	echo -e "\033[32m$1\033[0m"
}

pack_help()
{
	echo "###############################################"
	echo ""
	echo "   Usage:"
	echo "   	Build: $0 + version(eg:IOT_V1.01)"
	echo "   	Clean: $0 + clean"
	echo ""
	echo "###############################################"
}

build_clean()
{
	rm ${ROOTDIR}/bin/uboot.bin -rf
	rm ${ROOTDIR}/bin/ipcam.bin.zip -rf
	rm ${ROOTDIR}/bin/res.jffs2 -rf
	rm ${ROOTDIR}/bin/recovery.bin -rf
	rm ${ROOTDIR}/bin/mcu.bin -rf
	rm ${ROOTDIR}/bin/env.bin -rf
	rm ${ROOTDIR}/bin/envbak.bin -rf
	rm ${ROOTDIR}/bin/wifiboot.bin -rf
	rm ${ROOTDIR}/bin/wifikernel.bin -rf
}

build_uboot()
{
	ECHO ">>>>>> build uboot <<<<<<"
	echo "cp ${ROOTDIR}/res/uboot/${CLIENT_TYPE}/uboot.bin ${ROOTDIR}/bin/ -rfa"
	cp ${ROOTDIR}/res/uboot/${CLIENT_TYPE}/uboot.bin ${ROOTDIR}/bin/ -rfa
}

build_env()
{
	# 拷贝uboot的env数据,用于实现升级异常恢复,升级先烧写envbak.bin,最后恢复env.bin
	ECHO ">>>>>> build env <<<<<<"
	echo "cp ${ROOTDIR}/res/uboot/${CLIENT_TYPE}/env.bin ${ROOTDIR}/bin/ -rfa"
	echo "cp ${ROOTDIR}/res/uboot/${CLIENT_TYPE}/envbak.bin ${ROOTDIR}/bin/ -rfa"
	cp ${ROOTDIR}/res/uboot/${CLIENT_TYPE}/env.bin ${ROOTDIR}/bin/ -rfa
	cp ${ROOTDIR}/res/uboot/${CLIENT_TYPE}/envbak.bin ${ROOTDIR}/bin/ -rfa
}

build_ipcam()
{
	ECHO ">>>>>> build ipcam <<<<<<"
	echo "cp ${ROOTDIR}/../out/ipcam.bin.zip ${ROOTDIR}/bin/ -rfa"
	cp ${ROOTDIR}/../out/ipcam.bin.zip ${ROOTDIR}/bin/ -rfa
}

build_res()
{
	ECHO ">>>>>> build res <<<<<<"
	echo "cp ${ROOTDIR}/res/res_partion/${CLIENT_TYPE}/res.jffs2 ${ROOTDIR}/bin/ -rfa"
	cp ${ROOTDIR}/res/res_partion/${CLIENT_TYPE}/res.jffs2 ${ROOTDIR}/bin/ -rfa
}

build_recovery()
{
	ECHO ">>>>>> build recovery <<<<<<"
    if [ "$CLIENT_TYPE" == "B1" ]; then
        echo "cp ${ROOTDIR}/res/recovery/${CLIENT_TYPE}/recovery.bin_${PRODUCT_MODEL} ${ROOTDIR}/bin/recovery.bin -rfa"
        cp ${ROOTDIR}/res/recovery/${CLIENT_TYPE}/recovery.bin_${PRODUCT_MODEL} ${ROOTDIR}/bin/recovery.bin -rfa
    else
        echo "cp ${ROOTDIR}/res/recovery/${CLIENT_TYPE}/recovery.bin_B5 ${ROOTDIR}/bin/recovery.bin -rfa"
        cp ${ROOTDIR}/res/recovery/${CLIENT_TYPE}/recovery.bin_B5 ${ROOTDIR}/bin/recovery.bin -rfa
    fi
}

build_mcu()
{
	ECHO ">>>>>> build mcu <<<<<<"
	echo "cp ${ROOTDIR}/res/mcu/mcu.bin ${ROOTDIR}/bin/ -rfa"
	cp ${ROOTDIR}/res/mcu/mcu.bin ${ROOTDIR}/bin/ -rfa
}

build_wifiboot()
{
	ECHO ">>>>>> build wifi boot <<<<<<"
	echo "cp ${ROOTDIR}/res/wifi/wifiboot.bin ${ROOTDIR}/bin/ -rfa"
	cp ${ROOTDIR}/res/wifi/wifiboot.bin ${ROOTDIR}/bin/ -rfa
}

build_wifikernel()
{
	ECHO ">>>>>> build wifi kernel <<<<<<"
	echo "cp ${ROOTDIR}/res/wifi/wifikernel.bin ${ROOTDIR}/bin/ -rfa"
	cp ${ROOTDIR}/res/wifi/wifikernel.bin ${ROOTDIR}/bin/ -rfa
}

choise_product_model()
{
	echo ""
	echo "Choise Product Model:"
	echo " 1 - B1"
	echo " 2 - B5"
	echo " 3 - W5"
	echo " 4 - B7"
	echo ""

	echo -e "Please enter the Product Model index(default 2): \c"
	read input
	if [ -z $input ]; then
		input=2
	fi
	
	case $input in 
		1)
            
            echo "cp ${ROOTDIR}/packTools/${UPGRADE_TOOL_NAME}_b1 ${ROOTDIR}/bin/${UPGRADE_TOOL_NAME}"
            cp ${ROOTDIR}/packTools/${UPGRADE_TOOL_NAME}_b1 ${ROOTDIR}/bin/${UPGRADE_TOOL_NAME}
			PRODUCT_MODEL=B1
			;;
		2)
            echo "cp ${ROOTDIR}/packTools/${UPGRADE_TOOL_NAME}_b5 ${ROOTDIR}/bin/${UPGRADE_TOOL_NAME}"
            cp ${ROOTDIR}/packTools/${UPGRADE_TOOL_NAME}_b5 ${ROOTDIR}/bin/${UPGRADE_TOOL_NAME}
			PRODUCT_MODEL=B5
			;;
			
		3)
            echo "cp ${ROOTDIR}/packTools/${UPGRADE_TOOL_NAME}_b5 ${ROOTDIR}/bin/${UPGRADE_TOOL_NAME}"
            cp ${ROOTDIR}/packTools/${UPGRADE_TOOL_NAME}_b5 ${ROOTDIR}/bin/${UPGRADE_TOOL_NAME}
			PRODUCT_MODEL=W5
			;;
			
		4)
            echo "cp ${ROOTDIR}/packTools/${UPGRADE_TOOL_NAME}_b5 ${ROOTDIR}/bin/${UPGRADE_TOOL_NAME}"
            cp ${ROOTDIR}/packTools/${UPGRADE_TOOL_NAME}_b5 ${ROOTDIR}/bin/${UPGRADE_TOOL_NAME}
			PRODUCT_MODEL=B7
			;;
	esac

    echo $PRODUCT_MODEL
}

choise_client_type()
{
	echo ""
	echo "Choise Client Type:"
	echo " 1 - CN"
	echo " 2 - EN"
	echo " 3 - YD_MC"
	echo ""

	echo -e "Please enter the Client index(default 1): \c"
	read input
	if [ -z $input ]; then
		input=1
	fi
	
	case $input in 
		1)
			CLIENT_TYPE=CN
			;;
		2)
			CLIENT_TYPE=EN
			;;
		3)
			CLIENT_TYPE=MC
			;;
	esac

	PACK_VERSION=${PRODUCT_MODEL}_${CLIENT_TYPE}_${VERSION}
	
	echo $PACK_VERSION
}

choise_recovery_switch()
{
	echo ""
	echo "Choise recovery whether to support:"
	echo " 0 - NO"
	echo " 1 - YES"
	echo ""
	
	echo -e "Please enter the recovery support index(default 0): \c"
	read input
	if [ -z $input ]; then
		input=0
	fi
	
	case $input in 
		0)
			;;
		1)
			build_env
			;;
	esac
}

pack_package()
{
	cd ${ROOTDIR}/bin/
	# cp ${ROOTDIR}/packTools/${UPGRADE_TOOL_NAME} .
	
	./${UPGRADE_TOOL_NAME} ${PACK_TIME} ${PACK_VERSION} config
	if [ ! -e ../out ]; then
		mkdir -p ../out
	fi
	mv ${PACK_VERSION}.bin ../out/
	ECHO "Generate: \n\t${ROOTDIR}/out/${PACK_VERSION}.bin"
}

pack_main()
{
	build_clean

    choise_product_model
	choise_client_type
	choise_recovery_switch

	echo ""
	echo "Choise Pack Type:"
	echo "  1 - uboot"
	echo "  2 - ipcam" 
	echo "  3 - res"
	echo "  4 - recovery"
	echo "  5 - mcu" 	
	echo "  6 - wifiboot" 	
	echo "  7 - wifikernel" 	
	echo ""
	echo -e "Please enter the Pack index(default 2): \c"
	read input

	for i in $input
	do 
		case $i in 
			1)
				build_uboot;;
			2)
				build_ipcam;;
			3)
				build_res;;
			4)
				build_recovery;;
			5)
				build_mcu;;
			6)
				build_wifiboot;;
			7)
				build_wifikernel;;
			*)
				echo "input param is invalid !!!"
				exit;; 
		esac
	done

	if [ -z $i ]; then
		build_ipcam
	fi

	pack_package

	exit 0
}

if [ -z $1 ]
then
	pack_help
	exit 0
fi

pack_main

